// Plugins
import vue from '@vitejs/plugin-vue'
import vuetify, {transformAssetUrls} from 'vite-plugin-vuetify'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
// Utilities
import {defineConfig} from 'vite'
import {fileURLToPath, URL} from 'node:url'
//import inject from '@rollup/plugin-inject';  // 必须重要！效果和webpack.ProvidePlugin中相同
// console.log('apiUrl',process.env)
// https://vitejs.dev/config/
export default defineConfig({
  plugins: [
    vue({
      template: {transformAssetUrls},
      reactivityTransform: true,
    }),
    // 图片文件压缩
    // https://github.com/vuetifyjs/vuetify-loader/tree/master/packages/vite-plugin
    vuetify({
      autoImport: true,
      styles: {
        configFile: 'src/styles/settings.scss',
      },
    }),
    // 自动导入对应组件API
    // https://github.com/antfu/unplugin-auto-import
    AutoImport({
      imports: ['vue', '@vueuse/core', 'vue-router', 'pinia'],
      // hooks
      dirs: [
        './src/composables',
        './src/store/modules',
      ],
      dts: true,
      vueTemplate: true,
    }),
    // 自动导入对用路径组件
    // https://github.com/antfu/unplugin-vue-components
    Components({
      dirs: ['./src/components'],
      dts: true,
    }),
  ],
  define: {'process.env': {}},
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    },
    extensions: [
      '.js',
      '.json',
      '.jsx',
      '.mjs',
      '.ts',
      '.tsx',
      '.vue',
    ],
  },
  optimizeDeps: {
    // exclude: ['vuetify'], // 依赖预构建时会强制排除此类存在于node_modules中的依赖
    entries: [
      './src/**/*.vue',
    ],
  },
  build: {
    chunkSizeWarningLimit: 1500,
    rollupOptions: {
      treeshake: true,
      output: {
        // 缩小打包体积
        minifyInternalExports: true,
        manualChunks: (id) => {
          // 将 node_modules 中的代码单独打包成一个 JS 文件
          // hash code 不变，浏览器将缓存 node_modules 的文件
          if (id.includes('node_modules')) {
            if (/.*\.css$/.test(id)) {
              // 匹配是否将node_modules 中css打包
              console.log('css', id)
            }
            return 'vendor'
          }
        }
      }
    }
  },

  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: `https://mcabc.api.xintianyuehui.cn`,
        changeOrigin: true,
        rewrite: (path) => path.replace(/\/api/, "/api")
      },
      '/native': {
        target: `https://mcabc.api.xintianyuehui.cn`,
        changeOrigin: true,
        rewrite: (path) => path.replace(/\/native/, "")
      }
    }
  }
})
